<?php

namespace app\api\model\kuyou\applet;

use think\Model;


class Sets extends Model
{

    // 表名
    protected $name = 'kuyou_sets';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;

    /**
     * 获取设置
     * @param string $name
     * @return array
     */
    public static function getSet($name = null,$type=null)
    {
        //需修改用户协议，隐私协议到config表查询
        $list = collection(self::where(function ($query) use ($name,$type) {
            if (!is_null($name)) {
                if(strpos($name,',') !== false){
                    $name = explode(',',$name);
                    $query->where('name', 'in', $name);
                }else{
                    $query->where('name', '=', $name);
                }
            }
            if (!is_null($type)) {
                $query->where('type', '=', $type);
            }
        })->order('type','ASC')->field('id,type,name,nickname,value,notice,explain,is_checked')->select())->toArray();
        $data = ['1'=>'PC端','2'=>'小程序'];
        foreach ($list as $list_k=>$list_v){
            if($list_v['id']==20){
                $value = explode(',',$list_v['value']);
                $rowlist = [];
                if(in_array(1,$value)){
                    $rowlist[] = [
                        'id' => 1,
                        'name' => $data[1],
                        'is_checked' =>1
                    ];
                }else{
                    $rowlist[] = [
                        'id' => 1,
                        'name' => $data[1],
                        'is_checked' =>0
                    ];
                }
                if(in_array(2,$value)){
                    $rowlist[] = [
                        'id' => 2,
                        'name' => $data[2],
                        'is_checked' =>1
                    ];
                }else{
                    $rowlist[] = [
                        'id' => 2,
                        'name' => $data[2],
                        'is_checked' =>0
                    ];
                }
                $list[$list_k]['value_list'] = $rowlist;
            }
        }
        return $list;
    }

    /**
     * 获取设置
     * @param array $set
     * @return array
     */
    public static function setSet($set){
        if(empty($set)){
            return ['status'=>400,'msg'=>'参数错误'];
        }else{
            $data = [];
            foreach ($set as $set_k=>$set_v){
                if(!isset($set_v['name']) || (isset($set_v['name']) && empty($set_v['name'])) || !isset($set_v['value']) || !isset($set_v['is_checked'])){
                    return ['status'=>400,'msg'=>'参数错误'];
                }else{
                    $name = $set_v['name'];
                    $row = self::where(function ($query) use ($name) {
                        $query->where('name', '=', $name);
                    })->find();
                    if(empty($row)){
                        return ['status'=>405,'msg'=>'更新的设置不存在'];
                    }else if($row['name']=='base_currency' || $row['name']=='enable_time'){
                        return ['status'=>400,'msg'=>'此设置不可更改'];
                    }else if($row['name']=='price_decimal' && $set_v['value']<$row['value']){
                        return ['status'=>400,'msg'=>'价格小数位数不可小于已设置的值'];
                    }else if($row['name']=='number_decimal' && $set_v['value']<$row['value']){
                        return ['status'=>400,'msg'=>'数量小数位数不可小于已设置的值'];
                    }else if($row['name']=='valuation' && ($set_v['value']<1 || $set_v['value']>2)){
                        return ['status'=>400,'msg'=>'存货计价方法参数错误'];
                    }else if($set_v['name']=='price_taxes' && $set_v['is_checked']==1){
                        $taxes = 0;
                        foreach ($set as $k=>$v){
                            if($v['name']=='taxes'){
                                $taxes = $v['value'];
                            }
                        }
                        if($taxes==0){
                            $taxes = self::where(function ($query) {
                                $query->where('name', '=', 'taxes');
                            })->value('is_checked');
                        }
                        if(empty($taxes)){
                            return ['status'=>400,'msg'=>'必须启用税金才能设置商品价格是否含税'];
                        }
                        $data[] = [
                            'id'    => $row['id'],
                            'value' => $set_v['value'],
                            'is_checked' => empty($set_v['is_checked'])?0:1,
                        ];
                    }else if($row['name']=='discount' && $set_v['is_checked']==0){
                        $discount = self::where(function ($query) {
                            $query->where('name', '=', 'discount');
                        })->value('is_checked');
                        if($discount==1){
                            return ['status'=>400,'msg'=>'您已经启用了优惠金额，不可取消！'];
                        }
                    }else{
                        if(isset($set_v['value'])){
                            if(is_array($set_v['value'])){
                                $set_v['value'] = implode(',',$set_v['value']);
                            }
                            if(!isset($set_v['is_checked'])){
                                $set_v['is_checked'] = 0;
                            }
                        }
                        $data[] = [
                            'id'    => $row['id'],
                            'value' => $set_v['value'],
                            'is_checked' => empty($set_v['is_checked'])?0:1,
                        ];
                    }
                }
            }
            $res = (new Sets())->saveAll($data);
            if($res){
                return ['status'=>200,'msg'=>'更新设置成功'];
            }else{
                return ['status'=>500,'msg'=>'更新设置失败'];
            }
        }
    }
}
